package cn.example.demo.modules.house.recommend;

import java.util.DoubleSummaryStatistics;
import java.util.List;

/**
 * Description: 特征标准化处理器
 *
 * @author Lzx
 * @create 2025/3/22 16:22
 */
public class FeatureNormalizer {
    // Min-Max归一化
    public static void normalize(List<Double> features) {
        DoubleSummaryStatistics stats = features.stream()
                .mapToDouble(Double::doubleValue)
                .summaryStatistics();
        double min = stats.getMin();
        double range = stats.getMax() - min;
        if (range == 0) return;
        features.replaceAll(aDouble -> (aDouble - min) / range);
    }

    // Z-Score标准化
    public static void standardize(List<Double> features) {
        double mean = features.stream()
                .mapToDouble(Double::doubleValue)
                .average()
                .orElse(0.0);
        double std = Math.sqrt(features.stream()
                .mapToDouble(v -> Math.pow(v - mean, 2))
                .average()
                .orElse(0.0));
        if (std == 0) return;
        features.replaceAll(aDouble -> (aDouble - mean) / std);
    }
}
